#include "stdafx.h"
#include "caRStream.h"
#include "CMessageType.h"
#include "caMessageData.h"
#include "caRStream.h"
#include "caCreateHookManage.h"

PVOID caCCreateHookManage::Create()
{
	PVOID rdata[3];
	char* ldata[3];
	CMessageType mtype(MESSAGE_TYPE_CREATE);
	mtype<<*(new CMessageType(CREATE_HOOKCTRL))<<*(new caCMessageData(&rdata,sizeof(rdata)));

	char* name=new char[256];	
	caCRStream put(m_procctrl,STACK_MAX_SIZE);
	if(!put) return NULL;

	rdata[0]=(caRAddress)put;
	ldata[0]=(caLAddress)put;
	memset(name,0,256);
	_itoa_s(rand()*rand(),name,256,16);
	put.write(name,strlen(name)+1);

	rdata[1]=(caRAddress)put;
	ldata[1]=(caLAddress)put;
	memset(name,0,256);
	_itoa_s(rand()*rand(),name,256,16);
	put.write(name,strlen(name)+1);

	rdata[2]=(caRAddress)put;
	ldata[2]=(caLAddress)put;
	memset(name,0,256);
	_itoa_s(rand()*rand(),name,256,16);
	put.write(name,strlen(name)+1);
	
	if(!put.flush()) return NULL;

	HANDLE hCall=CreateEventA(NULL,FALSE,FALSE,ldata[0]);
	HANDLE hMap=CreateFileMappingA((HANDLE)0xFFFFFFFF,NULL,PAGE_EXECUTE_READWRITE,0,STACK_MAX_SIZE,ldata[2]);

	PVOID hook=(PVOID)m_procctrl->SendMessage(&mtype);
	CloseHandle(hCall);
	CloseHandle(hMap);
	return hook;
}

BOOL caCCreateHookManage::Destory(PVOID hookmange)
{

	CMessageType mtype(MESSAGE_TYPE_CREATE);
	mtype<<*(new CMessageType(DELETE_HOOKCTRL))<<*(new caCMessageData(&hookmange,sizeof(hookmange)));

	return (BOOL)m_procctrl->SendMessage(&mtype);
}